from rest_framework.response import Response
from rest_framework.views import APIView
from .order_serializers import OrderDetailSerializer, OrderSimpleSerializer
from meiduo_admin.my_paginations import MyPageNumberPagination
from orders.models import OrderInfo
from rest_framework.generics import ListAPIView, RetrieveAPIView


class OrderSimpleListView(ListAPIView):
    pagination_class = MyPageNumberPagination
    serializer_class = OrderSimpleSerializer

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword', '')
        return OrderInfo.objects.filter(order_id__contains=keyword)


class OrderDetailView(RetrieveAPIView):
    serializer_class = OrderDetailSerializer
    queryset = OrderInfo.objects.all()


class OrderUpdateStatusView(APIView):
    def put(self, request, order_id):
        status = request.data.get('status')
        if not status:
            return Response('missing argument', status=400)

        OrderInfo.objects.filter(order_id=order_id).update(status=status)

        return Response('update status successed', status=201)
